#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require


#include "../../../../core/shader_std.h"
#include "../../../../core/shader_built_in_functions.h"
#include "../../../../core/shader_mesh.h"


layout(push_constant, scalar) uniform DRAWPROP_push_constants {
	S_ConstPush_Mesh_Line const_push;
};

layout(std140, set = 0, column_major, binding = DEF_BINDING_PROJECT) uniform Buf0 {
	mat4 mat_ProjecView[4];
};


layout(std430, set = 1, scalar, column_major, binding = DEF_binding_L1_3D_Tranform) readonly buffer Buf1 {
	mat4 mat_Model[];
};


layout(location = 0) in vec3 inPos;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec3 inUV_Color;



out gl_PerVertex {
	vec4 gl_Position;
};

layout(location = 0) out vec4 outColor;
layout(location = 1) out vec3 outNormal;
layout(location = 2) out vec3 outViewLine;
layout(location = 3) out uint outShaderType;

vec3 pos[4] = {vec3(10.0, -10.0, 10), vec3(-100.0, 10.0,-2), vec3(10.0, 100.0,0), vec3(100.0, 100.0,0)};

void main(void){
	mat4 bonesMat_som = mat_Model[const_push.m_Offset_Tranform];

	mat4 rotMat = inverse(mat_ProjecView[DEF_Camera_Matrix_Rot]);
	//vec3 camPos = (inverse(mat_ProjecView[3]) * vec4(0,0,0,1)).xyz;
	vec4 p = vec4(inPos, 1);
	switch( (const_push.m_Ob_ShaderType >> 8) & 0xff ){
		case DEF_3DTranform_Normal : {
			
			break;
		}
		case DEF_3DTranform_LookAt : {
			p = rotMat * p;
			break;
		}
	}
	gl_Position = mat_ProjecView[DEF_Camera_Matrix_PV] * bonesMat_som * p;


	//gl_Position = mat_ProjecView[DEF_Camera_Matrix_Porj] * mat_ProjecView[DEF_Camera_Matrix_View] * bonesMat_som * p;
	//gl_Position = mat_ProjecView[DEF_Camera_Matrix_Porj] * vec4(0,0,0,1);
	outNormal = normalize(mat3(bonesMat_som) * inNormal);
	outViewLine = normalize(mat3(rotMat) * vec3(0,0,-1));
	outShaderType = const_push.m_Ob_ShaderType&0xff;

	switch ( outShaderType ) {
		case DEF_3DShaderType_FronWeight :
		case DEF_3DShaderType_PerObColor : {
			outColor = f_glsl_buildColor(const_push.m_Ob_Color);
			break;
		}
		case DEF_3DShaderType_VertAddObColor : {
			outColor = f_glsl_buildColor(floatBitsToUint(inUV_Color.z)) + f_glsl_buildColor(const_push.m_Ob_Color);
			break;
		}
		default:{
			outColor = f_glsl_buildColor(floatBitsToUint(inUV_Color.z));
		}
	}
	
	
	//gl_Position.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy / gl_Position.w;
	//gl_Position.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy / gl_Position.w;

	vec3 ndcpos = gl_Position.xyz / gl_Position.w;
	ndcpos.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy;
	gl_Position.xyz = ndcpos * gl_Position.w;
	


	//vec3 ndcposB = gl_Position.xyz / gl_Position.w;
	//ndcposB.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy;
	//ndcposB.z += 0.0001;
	//gl_Position.xyz = ndcposB * gl_Position.w;

}


